//
//  AppDelegate.swift
//  weibo001
//
//  Created by ios on 15/11/3.
//  Copyright © 2015年 ios. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
       
        print(UserAccountViewModel.sharedUserAccount.account)
        setuptintcolor()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor =  UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        print(isNewVersion)
        
        
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification,
            object: nil,
            queue: nil) { (NSNotification) -> Void in
                let vc = NSNotification.object != nil ? welcomeController() : MainViewController()
                
                self.window?.rootViewController = vc
        }
        
        return true
        
    }

    private func setuptintcolor() {
        
        UINavigationBar.appearance().tintColor = CCAppearanceTintColor
        UITabBar.appearance().tintColor = CCAppearanceTintColor
    }
   
}

//界面切换代码
extension AppDelegate {

// 启动的根视图控制器
    private var defaultRootViewController: UIViewController {
    
        if UserAccountViewModel.sharedUserAccount.UserLogin {
        
            return isNewVersion ? NewFeatureController() : welcomeController()
        }
        
        return MainViewController()
    }
    
    
// 当前版本
    private var isNewVersion: Bool{
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        
    print("当前版本\(version)")
    
// 之前版本
   let sandboxVersionKey = "sandboxVersionKey"
   let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
    print("之前版本\(sandboxVersion)")
        
        //保存当前版本
     NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandboxVersionKey)
        
    return version > sandboxVersion
    }
    
}
















